package stubs;

import java.util.Iterator;
import java.util.LinkedList;

import objetos.ItemVendaObj;
import interfaces.CarrinhoCompra;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import java.lang.Integer;

public class CarrinhoCompraStub implements CarrinhoCompra {

	@Override
	public boolean adicionarItem(ItemVendaObj item, int clienteId) {
		try {
			// Cria Endpoint para envio do xml
			EndpointReference targetEPR = new EndpointReference(Bind.getServicesLocation() + "CarrinhoCompra/adicionarItem");
			
			// Cria o arquivo xml
			OMFactory fac = OMAbstractFactory.getOMFactory();
			OMNamespace ns_par = fac.createOMNamespace("parametros", "par");
			
			OMElement payload = fac.createOMElement("parametros", ns_par);
			OMElement item_par = item.toOMElement();
			OMElement clientId_par = fac.createOMElement("clienteId", ns_par);
			
			clientId_par.setText(new Integer(clienteId).toString());
			payload.addChild(item_par);
			payload.addChild(clientId_par);
			
			System.out.println(payload);
			
			// Cria servico do cliente
			ServiceClient sc = new ServiceClient();
			
			// Cria opcoes do servico
			Options opt = new Options();
			opt.setTo(targetEPR);
			sc.setOptions(opt);
			
			// Envia e recebe resposta
			OMElement res;
			
			res = sc.sendReceive(payload);
			
			return Boolean.parseBoolean(res.getText());
			
		} catch (AxisFault e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean limparCarrinho(int clienteId) {
		try {
			// Cria Endpoint para envio do xml
			EndpointReference targetEPR = new EndpointReference(Bind.getServicesLocation() + "CarrinhoCompra/limparCarrinho");
			
			OMFactory fac = OMAbstractFactory.getOMFactory();
			OMNamespace ns_obj = fac.createOMNamespace("Objeto", "obj");
			OMElement payload = fac.createOMElement("objeto", ns_obj);
			
			payload.setText(new Integer(clienteId).toString());
			
			// Cria servico do cliente
			ServiceClient sc = new ServiceClient();
			
			// Cria opcoes do servico
			Options opt = new Options();
			opt.setTo(targetEPR);
			sc.setOptions(opt);
			
			// Envia e recebe resposta
			OMElement res;
			
			res = sc.sendReceive(payload);
			
			return Boolean.parseBoolean(res.getText());
			
		} catch (AxisFault e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public LinkedList<ItemVendaObj> produtosNoCarrinho(int clienteId) {
		try {
			// Cria Endpoint para envio do xml
			EndpointReference targetEPR = new EndpointReference(Bind.getServicesLocation() + "CarrinhoCompra/produtosNoCarrinho");
			
			OMFactory fac = OMAbstractFactory.getOMFactory();
			OMNamespace ns_obj = fac.createOMNamespace("atributo", "atb");
			OMElement payload = fac.createOMElement("clienteId", ns_obj);
			payload.setText(new Integer(clienteId).toString());
			
			// Cria servico do cliente
			ServiceClient sc = new ServiceClient();
			
			// Cria opcoes do servico
			Options opt = new Options();
			opt.setTo(targetEPR);
			sc.setOptions(opt);
			
			// Envia e recebe resposta
			OMElement res;
			
			res = sc.sendReceive(payload);
			
			LinkedList<ItemVendaObj> list = new LinkedList<ItemVendaObj>();
			Iterator<OMElement> it = res.getChildElements();
			while (it.hasNext()) {
				ItemVendaObj item = new ItemVendaObj();
				item.loadOMElement(it.next());
				list.add(item);
			}
			
			return list;
			
		} catch (AxisFault e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}

	@Override
	public boolean removerItem(ItemVendaObj item, int clienteId) {
		try {
			// Cria Endpoint para envio do xml
			EndpointReference targetEPR = new EndpointReference(Bind.getServicesLocation() + "CarrinhoCompra/removerItem");
			
			OMFactory fac = OMAbstractFactory.getOMFactory();
			OMNamespace ns_obj = fac.createOMNamespace("parametros", "par");
			OMElement payload = fac.createOMElement("parametros", ns_obj);
			OMElement clienteId_par = fac.createOMElement("clienteId", ns_obj);
						
			clienteId_par.setText(new Integer(clienteId).toString());
			payload.addChild(item.toOMElement());
			payload.addChild(clienteId_par);
			
			// Cria servico do cliente
			ServiceClient sc = new ServiceClient();
			
			// Cria opcoes do servico
			Options opt = new Options();
			opt.setTo(targetEPR);
			sc.setOptions(opt);
			
			// Envia e recebe resposta
			OMElement res;
			res = sc.sendReceive(payload);
			
			return Boolean.parseBoolean(res.getText());
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return false;
	}

}
